import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:mybaseplus/base/tool/empty/FFEmpty.dart';
import 'package:mybaseplus/base/tool/entitylog/MyEntityLog.dart';
import 'package:mybaseplus/base/tool/file/FFDirectory.dart';
import 'package:mybaseplus/base/tool/file/FFile.dart';

class MyPageTestRun{

  static Future<void> createPageCode({
    required String templateFile,
    required String targetDir,
    required String templateClassName,
    required String className,
    bool? forceFlush = false, //慎用
  }) async {

    if (FFEmpty.stringIsEmpty(targetDir)) {
      debugPrint("targetDir 参数不能为空");
      return;
    }

    if (!targetDir.endsWith(Platform.pathSeparator)) {
      targetDir = "${targetDir}${Platform.pathSeparator}";
    }

    bool isFileExist = await FFile.isExistFile(templateFile);
    if (isFileExist == false) {
      debugPrint("模板文件不存在 ${templateFile}");
      return;
    }

    String classCode = await FFile.readAsString(templateFile);

    if (FFEmpty.stringIsEmpty(classCode)) {
      debugPrint("模板文件内容不应该为空 ${templateFile}");
      return;
    }

    if (!classCode.contains(templateClassName)) {
      debugPrint("模板文件不包含类名${templateClassName} ${templateFile}");
      return;
    }

    classCode = classCode.replaceAll(templateClassName, className);
    String filePath = "$targetDir$className.dart";

    if (forceFlush == true) {
      FFile.writeContentByPath(filePath: filePath, content: classCode);
    } else {

      bool exist = await FFile.isExistFile(filePath);
      if (exist) {
        debugPrint("路径下类已经存在 ${filePath}");
      } else {
        bool success = await FFile.writeContentByPath(filePath: filePath, content: classCode,autoCreate: true);
        if (success == true) {
          debugPrint("路径下类生成成功 ${filePath}");
        }  else {
          debugPrint("路径下类生成失败 ${filePath}");
        }

      }
    }
  }

  static Future<String> createPageCode2({
    required String templateFile,
    required String targetDir,
    required String templateClassName,
    required String className,
    bool? forceFlush = false, //慎用
  }) async {

    if (FFEmpty.stringIsEmpty(targetDir)) {
      return "targetDir 参数不能为空";
    }

    if (!targetDir.endsWith(Platform.pathSeparator)) {
      targetDir = "${targetDir}${Platform.pathSeparator}";
    }

    bool isFileExist = await FFile.isExistFile(templateFile);
    if (isFileExist == false) {
      return "模板文件不存在 ${templateFile}";
    }

    String classCode = await FFile.readAsString(templateFile);

    if (FFEmpty.stringIsEmpty(classCode)) {
      return "模板文件内容不应该为空 ${templateFile}";
    }

    if (!classCode.contains(templateClassName)) {
      return "模板文件不包含类名${templateClassName} ${templateFile}";
    }

    classCode = classCode.replaceAll(templateClassName, className);
    String filePath = "$targetDir$className.dart";

    if (forceFlush == true) {
      FFile.writeContentByPath(filePath: filePath, content: classCode);
    } else {

      bool exist = await FFile.isExistFile(filePath);
      if (exist) {
        return "路径下类已经存在 ${filePath}";
      } else {
        bool success = await FFile.writeContentByPath(filePath: filePath, content: classCode,autoCreate: true);
        if (success == true) {
          return "路径下类生成成功 ${filePath}";
        }  else {
          return "路径下类生成失败 ${filePath}";
        }

      }
    }

    return "异常错误";
  }
}